<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${pkgName}.${className}Mapper">
    <resultMap id="${className}ResultMap" type="${pkgName?replace("dao", "model")}.${className}">
        <id column="ID" property="id" jdbcType="VARCHAR"/>
        <#list propColTypeList as pct>
        <result column="${pct.columnName!''}" property="${pct.propertyName!''}" jdbcType="${pct.jdbcType!''}"/>
        </#list>
    </resultMap>
    
    <sql id="Base_Column_List">
        ID<#list propColTypeList as pct>, ${pct.columnName!''}</#list>
    </sql>
    
    <sql id="Base_Property_List">
        ${r"#"}{id, jdbcType=VARCHAR} <#list propColTypeList as pct>, ${r"#"}{${pct.propertyName!''}, jdbcType=${pct.jdbcType!''}}</#list>
    </sql>
    
    <sql id="sql_condition">
        <#list propColTypeList as pct>
          <if test="${pct.propertyName} != null and ${pct.propertyName} != ''">AND ${pct.columnName} = ${r"#"}{${pct.propertyName}, jdbcType=${pct.jdbcType!''}}</if>
        </#list>
    </sql>
    
    <sql id="sql_update_set">
      <trim prefix="SET" prefixOverrides=",">
      <#list propColTypeList as pct>
        <if test="${pct.propertyName} != null and ${pct.propertyName} != ''">,${pct.columnName} = ${r"#"}{${pct.propertyName}, jdbcType=${pct.jdbcType}}</if>
      </#list>
      </trim>
    </sql>
    
    <select id="findAll" resultMap="${className}ResultMap">
        select <include refid="Base_Column_List" /> from ${tableName}
    </select>
    
    <select id="findById" resultMap="${className}ResultMap" parameterType="java.lang.String">
        select <include refid="Base_Column_List" /> 
        from ${tableName} 
        where ID = ${r"#"}{id,jdbcType=VARCHAR}
    </select>
    
    <insert id="insert" parameterType="${pkgName?replace("dao", "model")}.${className}">
        insert into ${tableName}
        (<include refid="Base_Column_List" />)
        values
        (<include refid="Base_Property_List" />)
    </insert>
    
    <update id="update" parameterType="java.util.Map">
        UPDATE ${tableName}
        <include refid="sql_update_set" />
        <trim prefix="WHERE" prefixOverrides="AND">
        <include refid="sql_condition" />
        </trim>
    </update>
    
    <update id="update${className}ById" parameterType="${pkgName?replace("dao", "model")}.${className}">
        update ${tableName} 
        <include refid="sql_update_set" />
        where ID = ${r"#"}{id, jdbcType=VARCHAR}
    </update>
    
    <delete id="delete" parameterType="java.lang.String">
        delete from ${tableName} where ID = ${r"#"}{id, jdbcType=VARCHAR}
    </delete>
</mapper>